// pages/elderList/elderList.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    count:0,
    lastElderId:'',
    currentTime:'',
    msgCount:0,
    active: 0,
    recordList:[],
    elderList:
    [
      
    ],
    realTime:null
   
  },
  onChange(event) {
    console.log(event.detail)
    this.setData({
      active:event.detail
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.createRecord()
  },
  createRecord(){
    wx.request({
      url: 'https://zhixiao.site:8080/medication-record/generate',
      success:(res)=>{
      },
      complete:(res)=>{
        this.queryElders()
      }
    })
  },
  
  queryElders(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/names',
      data:{
        id:wx.getStorageSync('userid')
      },
      success:(res)=>{
        console.log(res)
      },
      complete:(res)=>{
        if(res.data.data===null){
          
          return
        }
        console.log(res.data.data)
        var elders = res.data.data
        var elderIds = []
        for(var i in elders){
          elderIds.push(Object.keys(elders[i])[0])
        }
        
        
        console.log(elderIds)
        this.setData({
          elderIds:elderIds
        })
        for (var i in this.data.elderIds){
          this.queryMedicineList(this.data.elderIds[i],i)
        }
       
      }
    })
  },
  queryMedicineList(elderId,i){
    wx.request({
      url: 'https://zhixiao.site:8080/client/mlist',
      data:{
        elderId:elderId,
        nurseId:wx.getStorageSync('userid')
      },
      success:(res)=>{
      },
      complete:(res)=>{
        var medicineList = res.data.data
        var list_num = new Set()
        var ids = []
        for (var i in medicineList){
          var listId = medicineList[i].id
         
          list_num.add(listId)
        }
        var list_num1 = Array.from(list_num)
        this.setData({
          list_num:list_num1
        })
   
        if(this.data.list_num.length===0){
          return
        }
        
        this.queryDetails(i)
      }
    })
  },
  
  queryDetails(index){
    wx.request({
      url: `https://zhixiao.site:8080/client/details?ids=${this.data.list_num}`,
      method:'POST',
      header:{
        "Content-Type": "application/json"
      },
      success:(res)=>{
        var recordList = this.data.recordList
       
        var recordList1 = res.data.data
        for(var i=0;i<recordList1.length;i++){
          recordList.push(recordList1[i])
        }
        console.log(999)
        console.log(recordList)
        this.setData({
          recordList:recordList,
        })
      },
      complete:(res)=>{
         var count = this.data.count
         count += 1
         this.setData({
           count:count
         })
         if(count == this.data.elderIds.length){
            console.log("hahahha")
            var date = new Date()
            var time = date.getHours()
            var minute = date.getMinutes()
            if(time<10){
              time = `0${time}`
            }
            if(minute<10){
              minute = `0${minute}`
            }
            var time1 = `${time}:${minute}:00`
            this.setData({
              currentTime:time1
            })
            var recordList = this.data.recordList
           
            for(var i=0;i<recordList.length;i++){
              if(recordList[i].time<=this.data.currentTime){
                var msgCount = this.data.msgCount + 1
                this.setData({
                  msgCount:msgCount
                })
              }
            }
            wx.setStorageSync('msgCount', msgCount)
         }
      }
    })
  },
  query_elderList(){
    // var workerId = wx.getStorageSync('workerId')
    wx.request({
      url: 'https://zhixiao.site:8080/client/query_elderList',
      data:
      {
        workerId:wx.getStorageSync('userid'),
      },
      success:(res)=>{
        console.log(res)
        var elderList = res.data.data
        this.setData({
          elderList:elderList
        })
        
        
        
        
      },
      fail:()=>{
        console.log("查询失败")
      }
    })
  },

  navigator_binding(){
    wx.navigateTo({
      url: '/subpkg_worker/my/bindelder/bindelder',
    })
  },
  afterRead(event) {
    var index = event.currentTarget.dataset.index
    const elderlyId = this.data.elderList[index].elderlyId
    const { file } = event.detail;
    const elderList = this.data.elderList
    elderList[index].image = file.tempFilePath
    this.setData({
      elderList:elderList
    })
    wx.request({
      url: 'https://zhixiao.site:8080/client/elder/upload',
      data:{
        elderId:elderlyId,
        image:file.tempFilePath
      },
      success:(res)=>{
        console.log("success")
      }
    })
  },
  navigator(e){
    
   
    var index = e.currentTarget.dataset.index
    console.log(index)
    var elderMsg = this.data.elderList[index]
    wx.navigateTo({
      url: `/subpkg_worker/record/record?id=${elderMsg.elderlyId}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.query_elderList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    if(this.data.elderList==null){
      Toast('未绑定老人')
    }
    const that = this
    var realTime = setInterval(function(){
      that.query_elderList()
      console.log('循环请求中')
    },5000)
    this.setData({
      realTime:realTime
    })
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    var realTime = this.data.realTime
    clearInterval(realTime)
    this.setData({
      realTime:realTime
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var realTime = this.data.realTime
    clearInterval(realTime)
    this.setData({
      realTime:realTime
    })
  },

  searchName(){
    wx.navigateTo({
      url: '/subpkg_worker/search/searchName/searchName',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.request({
      url: 'https://zhixiao.site:8080/client/query_elderList',
      data:
      {
        workerId:wx.getStorageSync('userid'),
      },
      success:(res)=>{
        console.log(res)
        var elderList = res.data.data
        this.setData({
          elderList:elderList
        })
        console.log(this.data.elderList)
      },
      fail:()=>{
        console.log("查询失败")
      },
      complete:(res)=>{
        wx.stopPullDownRefresh()
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})